package com.xiaomei.manager.web.controller;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import com.xiaomei.manager.model.Authinfo;
import com.xiaomei.manager.service.IAuthinfoService;
import com.xiaomei.manager.service.IExperienceService;
import com.xiaomei.manager.web.dto.AuthinfoAndUserDto;
import com.xiaomei.manager.web.dto.req.AuthQueryDto;
import com.xiaomei.manager.web.page.Page;

@Controller
@RequestMapping(value="/driverAuth")
public class DriverAuthController {
	
	@Autowired
	private IAuthinfoService ianthinfoService;
	@Autowired
    private IExperienceService experiService;
	
	@RequiresPermissions("driverAuth:view")
	@RequestMapping()
	public String list(HttpServletRequest request,AuthQueryDto dto,Page<AuthinfoAndUserDto> page){
		ianthinfoService.getAuthinfoList(dto, page);
		request.setAttribute("dto", dto);
		request.setAttribute("page", page);
		return "/auth/list";
	}
	
	
	@RequiresPermissions("driverAuth:update")
	@RequestMapping(value = "/{id}/update",method = RequestMethod.GET)
	public String detail(@PathVariable("id") int id, Model model){
		AuthinfoAndUserDto selectByPrimaryKey = (AuthinfoAndUserDto)ianthinfoService.selectByPrimary(id);
		model.addAttribute("Authinfo", selectByPrimaryKey);
		model.addAttribute("expList", experiService.selectByTicket("2"));
		return "/auth/detail";
	}
	
	@RequiresPermissions("driverAuth:update")
	@RequestMapping(value = "/{id}/update",method = RequestMethod.POST)
	public String update(Authinfo info, RedirectAttributes redirectAttributes){
		Integer expID = info.getExpID();
		String userCode = info.getUserCode();
		if(expID != null && userCode != null && !"".equals(userCode)){
			experiService.distributeTicket(userCode, expID);
		}
		info.setAduitTime(new Timestamp(System.currentTimeMillis()));
		ianthinfoService.updateByPrimaryKey(info);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return "redirect:/driverAuth";
	}
	
	
}
